<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shop_Category_Model extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('entity/shop_category_entity', 'Shop_Category_Entity');
    }

    public function getAll($option = array(), $aSelect=array(), $shop_id=-1)
    {
        $select = "
        a.id, a.title, a.slug, a.cate_id, a.shop_id, a.order, a.created_at, a.deleted_at,
        b.title AS shop_cate_title, b.slug AS shop_cate_slug,
        d.title AS cate_title, d.slug AS cate_slug";
        if(count($aSelect)>0){
            $select = implode(',', $aSelect);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_shop_category_name . ' AS a');
        $this->db->join($this->table_shop_name . ' b', 'a.shop_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'b.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        if ($shop_id!=-1) {
            $this->db->where('a.shop_id', $shop_id);
        }
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->order_by('a.order', 'ASC');
        $this->db->order_by('a.created_at', 'DESC');
        if (count($option) > 0) {
            $this->db->where($option);
        }
        return $this->db->get()->result('Shop_Category_Entity');
    }

    /**
     * if admin => manual use shop_id 
     * get a category
     */
    public function get($slug, $shop_id = -1)
    {
        $select = "
        a.id, a.title, a.slug, a.cate_id, a.shop_id, a.order, a.deleted_at,
        d.title AS cate_title, d.slug AS cate_slug";
        $this->db->select($select, true);
        $this->db->from($this->table_shop_category_name . ' AS a');
        $this->db->join($this->table_shop_name . ' b', 'a.shop_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'b.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('a.slug', $slug);
        if($shop_id!=-1){
            $this->db->where('a.shop_id', $shop_id);
        }
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        return $this->db->get()->first_row('Shop_Category_Entity');
    }

    /**
     * For admin only
     * Add new category
     */
    public function insert($category)
    {
        $this->db->trans_begin();
        $this->db->insert($this->table_shop_category_name, $category);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a category
     */
    public function update($category_id, $category)
    {
        $this->db->trans_begin();
        $this->db->where('id', $category_id);
        $this->db->update($this->table_shop_category_name, $category);
        if ($this->db->trans_status() === false) {
            $this->db->trans_rollback();
            return false;
        } else {
            $this->db->trans_commit();
        }
        return true;
    }

    /**
     * For admin only
     * update a category
     */
    public function checkDuplicateCategory($shop_id, $cate, $curId = 0)
    {
        $this->db->from($this->table_shop_category_name);
        $this->db->where('shop_id', $shop_id);
        if ($curId > 0) {
            $this->db->where('id <>', $curId);
        }
        $this->db->where("(slug='{$cate['slug']}' OR title='{$cate['title']}')");
        return $this->db->count_all_results();
    }

    /**
     * For admin only
     * update a category
     */
    public function getParentCategory($id){
        $select = "d.id, d.title, d.slug";
        $this->db->select($select, true);
        $this->db->from($this->table_shop_category_name . ' AS a');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('d.deleted_at', null);
        $this->db->where('a.id', $id);
        $this->load->model('entity/category_entity', 'Category_Entity');
        return $this->db->get()->first_row('Category_Entity');
    }
}
?>